<template>
  <div class="app-container">
    <div class="contentDiv">
      <div class="tx-r" style="margin-bottom: 10px">
        <el-button v-permiss="['702010']" size="small" type="primary" :loading="saveloading" round :disabled="balanceStatus == 0" @click="saveData(0)">保存</el-button>
        <el-button size="small" type="primary" :disabled="balanceStatus == 0" round @click="showSum">试算平衡汇总</el-button>
        <el-button v-permiss="['704020']" size="small" type="primary" :disabled="balanceStatus == 0" round @click="handImport">导入</el-button>
      </div>
      <el-table v-loading="listLoading" class="balance" :data="tableData" stripe fit resize empty-text="暂无相关数据" style="width: 100%;" :height="tableHeight">
        <el-table-column label="科目编码" min-width="90">
          <template slot-scope="scope">
            <span class="tabletxt">{{ scope.row.coaCode }}</span>
            <el-button v-if="scope.row.leaf == 1 && scope.row.type ==1 && scope.row.isAuxiliary == 1 && balanceStatus == 1" type="primary" icon="el-icon-plus" circle size="mini" style="margin-left:10px" @click="showSuplyConfig(scope.$index)" />
            <el-button v-if="scope.row.leaf == 1 && scope.row.type !=1 && scope.row.isAuxiliary == 1 && balanceStatus == 1" type="danger" icon="el-icon-delete" circle size="mini" style="margin-left:10px" @click="removeRow(scope.$index)" />
          </template>
        </el-table-column>
        <el-table-column label="科目名称" min-width="110" style="padding-right:100px;" show-overflow-tooltip>
          <template slot-scope="scope">
            <span class="plr10 tabletxt">{{ scope.row.showCoaName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="方向" width="100" align="center">
          <template slot-scope="{row}">
            <span class="tabletxt">{{ row.crDr==1?'借':row.crDr==0?'平':'贷' }}</span>
          </template>
        </el-table-column>
        <!-- type0为余额表节点 1为科目节点 -->
        <el-table-column :label="'期初余额('+userInfo.book.enablePeriodCode+')'" min-width="240">
          <el-table-column label="金额(元)" min-width="80" align="right">
            <template slot-scope="scope">
              <input v-model.trim="scope.row.beginBalance" class="tx-r" :disabled="scope.row.leaf==0||(scope.row.isAuxiliary==1&&scope.row.type==1) || balanceStatus == 0" @focus="focusThis($event)" @input="inputChange($event, 'beginBalance', scope.$index)" @change="valChange(scope.row)" />
            </template>
          </el-table-column>
          <el-table-column label="数量" min-width="80" align="right">
            <template slot-scope="scope">
              <input v-model.trim="scope.row.beginBalanceQty" class="tx-r" :disabled="scope.row.leaf==0||(scope.row.isAuxiliary==1&&scope.row.type==1) || balanceStatus == 0" @focus="focusThis($event)" @input="inputChange($event, 'beginBalanceQty', scope.$index)" @change="valChange(scope.row)" />
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column :label="'本年借方累计'+ (userInfo.book.enablePeriodNum > 1 ? '(1-' + (userInfo.book.enablePeriodNum - 1) + '月)':'')" min-width="240">
          <el-table-column label="金额(元)" min-width="80" align="right">
            <template slot-scope="scope">
              <input v-model.trim="scope.row.yearNetDr" class="tx-r" :disabled="scope.row.leaf==0||(scope.row.isAuxiliary==1&&scope.row.type==1) || userInfo.book.enablePeriodNum == 1 || balanceStatus == 0" @focus="focusThis($event)" @input="inputChange($event, 'yearNetDr', scope.$index)" @change="valChange(scope.row)" />
            </template>
          </el-table-column>
          <el-table-column label="数量" min-width="80" align="right">
            <template slot-scope="scope">
              <input v-model.trim="scope.row.yearNetQtyDr" class="tx-r" :disabled="scope.row.leaf==0||(scope.row.isAuxiliary==1&&scope.row.type==1) || userInfo.book.enablePeriodNum == 1 || balanceStatus == 0" @focus="focusThis($event)" @input="inputChange($event, 'yearNetQtyDr', scope.$index)" @change="valChange(scope.row)" />
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column :label="'本年贷方累计'+ (userInfo.book.enablePeriodNum > 1 ? '(1-' + (userInfo.book.enablePeriodNum - 1) + '月)':'')" min-width="240">
          <el-table-column label="金额(元)" min-width="80" align="right">
            <template slot-scope="scope">
              <input v-model.trim="scope.row.yearNetCr" class="tx-r" :disabled="scope.row.leaf==0||(scope.row.isAuxiliary==1&&scope.row.type==1) || userInfo.book.enablePeriodNum == 1 || balanceStatus == 0" @focus="focusThis($event)" @input="inputChange($event, 'yearNetCr', scope.$index)" @change="valChange(scope.row)" />
            </template>
          </el-table-column>
          <el-table-column label="数量" min-width="80" align="right">
            <template slot-scope="scope">
              <input v-model.trim="scope.row.yearNetQtyCr" class="tx-r" :disabled="scope.row.leaf==0||(scope.row.isAuxiliary==1&&scope.row.type==1) || userInfo.book.enablePeriodNum == 1 || balanceStatus == 0" @focus="focusThis($event)" @input="inputChange($event, 'yearNetQtyCr', scope.$index)" @change="valChange(scope.row)" />
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :close-on-click-modal="false" title="试算平衡汇总" :visible.sync="dialogVisible1" width="720px">
      <el-table :data="[balanceObj]" border fit resize empty-text="无同步数据" style="width: 100%;" size="small">
        <el-table-column align="center" label="项目">
          <span>金额</span>
        </el-table-column>
        <el-table-column align="center" label="期初借方">
          <template slot-scope="{row}">
            <span>{{ row.sumBalanceDr }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="期初贷方">
          <template slot-scope="{row}">
            <span>{{ row.sumBalanceCr }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="差额">
          <template slot-scope="{row}">
            <span>{{ row.diffBalance }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="借方发生额">
          <template slot-scope="{row}">
            <span>{{ row.sumYearNetDr }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="贷方发生额">
          <template slot-scope="{row}">
            <span>{{ row.sumYearNetCr }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="差额">
          <template slot-scope="{row}">
            <span>{{ row.diffYearNet }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否平衡">
          <template slot-scope="{row}">
            <span>{{ row.diffBalance == 0 && row.diffYearNet == 0?'平衡':'不平衡' }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" title="设置辅助核算" :visible.sync="dialogVisible2" width="660px">
      <el-form ref="dataForm" class="auditform" :rules="rules" :model="form" :inline="true" label-position="top" style="width: 600px; margin-left:15px;">
        <el-form-item label="期初余额" prop="beginBalance">
          <el-input v-model="form.beginBalance" size="small" placeholder="期初余额" style="width:185px" />
        </el-form-item>
        <el-form-item label="本年累计借方金额" prop="yearNetDr">
          <el-input v-model="form.yearNetDr" size="small" placeholder="本年累计借方金额" style="width:185px" :disabled="userInfo.book.enablePeriodNum == 1" />
        </el-form-item>
        <el-form-item label="本年累计贷方金额" prop="yearNetCr">
          <el-input v-model="form.yearNetCr" size="small" placeholder="本年累计贷方金额" style="width:185px" :disabled="userInfo.book.enablePeriodNum == 1" />
        </el-form-item>
        <el-form-item label="期初数量" prop="beginBalanceQty">
          <el-input v-model="form.beginBalanceQty" size="small" placeholder="期初数量" style="width:185px" />
        </el-form-item>
        <el-form-item label="本年累计借方数量" prop="yearNetQtyDr">
          <el-input v-model="form.yearNetQtyDr" size="small" placeholder="本年累计借方数量" style="width:185px" :disabled="userInfo.book.enablePeriodNum == 1" />
        </el-form-item>
        <el-form-item label="本年累计贷方数量" prop="yearNetQtyCr">
          <el-input v-model="form.yearNetQtyCr" size="small" placeholder="本年累计贷方数量" style="width:185px" :disabled="userInfo.book.enablePeriodNum == 1" />
        </el-form-item>
        <el-form-item v-for="it in auxiliaryCoaArr" :key="it.auxiliaryTypeCode" :label="it.auxiliaryTypeName" :prop="it.auxiliaryTypeCode" style="margin-right:13px">
          <auxList :ref="it.auxiliaryTypeCode+'Select'" :val.sync="form[it.auxiliaryTypeCode+'Id']" :code="it.auxiliaryTypeCode" :width="185" />
        </el-form-item>
      </el-form>
      <div v-if="balanceStatus == 1" slot="footer" class="dialog-footer" align="center">
        <el-button @click="dialogVisible2 = false">取消</el-button>
        <el-button type="primary" @click="saveAuxiliaryConfig">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="导入期初余额" :visible.sync="dialogVisible3" width="450px">
      <el-form ref="importForm" :model="importForm" label-position="left" label-width="0" style="margin-left:10px;">
        <el-form-item label prop="fileName" :rules="fileRule">
          <el-upload dragle action="/" accept=".xls, .xlsx" :auto-upload="false" :show-file-list="false" :file-list="fileList" :on-change="importFile">
            <div class="fileDiv">
              <span v-if="importForm.fileName" style="margin-right:5px">{{ importForm.fileName }}</span>
              <el-button type="text">{{ fileList.length === 0 ? '点击上传' : '重新上传' }}</el-button>
            </div>
          </el-upload>
        </el-form-item>
        <div style="height:32px" class="tx-l">
          <el-button type="text" @click="downloadTemplate">下载模板</el-button>
        </div>
        <div class="tx-l" style="color:red;font-size:13px;line-height:20px;margin-top:15px">重要提示：导入操作会删除已保存的期初余额和本年累计，保留本期发生额。</div>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button size="small" @click="dialogVisible3 = false">取消</el-button>
        <el-button type="primary" size="small" @click="handleImport()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { getBalance, getPeriodList, importBalance, updateListForSetBegin, getOpeningBalanceStatus } from '@/api/user'
import { deepClone, toNumStr, validateVal } from '@/utils'
import auxList from '@/components/voucher/auxList'
var userInfo = JSON.parse(sessionStorage.userInfo)
export default {
  name: 'InitialBalance',
  components: { auxList },
  data() {
    return {
      listQuery: {
        periodCode1: '',
        isOnlyAdjustNet: '1'
      },
      dialogVisible1: false,
      dialogVisible2: false,
      curShowIndex: 0,
      auxiliaryTypeArr: [],
      saveloading: false,
      auxiliaryData: [],
      tableHeight: document.body.clientHeight - 183,
      form: {
        auxiliary: '0000000000000000000',
        beginBalance: 0,
        yearNetDr: 0,
        yearNetCr: 0,
        beginBalanceQty: 0,
        yearNetQtyDr: 0,
        yearNetQtyCr: 0
      },
      auxiliaryCoaArr: [],
      resetform: {
        beginBalance: 0,
        yearNetDr: 0,
        yearNetCr: 0,
        beginBalanceQty: 0,
        yearNetQtyDr: 0,
        yearNetQtyCr: 0
      },
      rules: {
        beginBalance: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        yearNetDr: [{ required: true, message: '不能为空', trigger: 'change' }],
        yearNetCr: [{ required: true, message: '不能为空', trigger: 'change' }],
        beginBalanceQty: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        yearNetQtyDr: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        yearNetQtyCr: [
          { required: true, message: '不能为空', trigger: 'change' }
        ]
      },
      userInfo: userInfo,
      periodList: [],
      tableData: [],
      listLoading: false,
      balanceStatus: 1,
      balanceObj: {},
      dialogVisible3: false,
      importForm: {
        fileName: ''
      },
      fileRule: [
        { required: true, message: '请选择要导入的文件！', trigger: 'change' }
      ],
      fileList: []
    }
  },
  computed: {
    ...mapGetters(['auxiliaryArr'])
  },
  mounted() {
    this.getData()
    this.getBalanceStatus()
    this.$store.dispatch('voucher/getAuxiliaryTypeList').then(() => {
      this.auxiliaryArr.forEach(item => {
        this.rules[item.auxiliaryTypeCode] = [{ required: true, message: '不能为空', trigger: 'change' }]
      })
    })
  },
  methods: {
    downloadTemplate() {
      location.href = 'business/balance.xlsx'
    },
    handImport() {
      this.dialogVisible3 = true
    },
    importFile(file, fileList) {
      this.formData = new FormData()
      if (fileList.length > 0) {
        this.fileList = [fileList[fileList.length - 1]]
      }
      this.formData.append('file', file.raw)
      this.importForm.fileName = file.name
      this.$refs.importForm.validateField('fileName')
    },
    handleImport() {
      this.$refs.importForm.validate((valid) => {
        if (valid) {
          const obj = this.formData
          importBalance(obj).then((res) => {
            if (res.data.errorCode === '0') {
              this.$message.success('导入成功')
              this.dialogVisible3 = false
              this.getData()
            }
          })
        }
      })
    },
    inputChange(e, param, index) {
      var num = validateVal(e.currentTarget.value)
      this.$set(this.tableData[index], param, num)
    },
    focusThis(e) {
      e.currentTarget.select()
    },
    getPeriod() {
      getPeriodList().then((res) => {
        this.periodList = res.data.data
        this.listQuery.periodCode1 = res.data.data[0].id
        this.getData('0')
      })
    },
    getBalanceStatus() {
      var obj = {
        periodCode: userInfo.book.enablePeriodCode
      }
      getOpeningBalanceStatus(obj).then((res) => {
        if (res.data.errorCode === '0') {
          this.balanceStatus = res.data.data
        }
      })
    },
    showSum() {
      this.getBalanceObj()
      this.dialogVisible1 = true
    },
    removeRow(index) {
      var row = this.tableData[index]
      this.$set(row, 'beginBalance', 0)
      this.$set(row, 'beginBalanceQty', 0)
      this.$set(row, 'yearNetDr', 0)
      this.$set(row, 'yearNetQtyDr', 0)
      this.$set(row, 'yearNetCr', 0)
      this.$set(row, 'yearNetQtyCr', 0)
      this.calculate(row)
      this.$nextTick(() => {
        this.tableData.splice(index, 1)
      })
    },
    getBalanceObj() {
      var obj = {
        sumBalanceDr: 0,
        sumBalanceCr: 0,
        sumYearNetDr: 0,
        sumYearNetCr: 0,
        diffBalance: 0,
        diffYearNet: 0
      }
      const arr = this.tableData
      for (var i = 0; i < arr.length; i++) {
        if (
          arr[i].leaf === 0 ||
          (arr[i].type === 1 && Number(arr[i].isAuxiliary) === 1)
        ) {
          continue
        }
        var tmpCrDr = arr[i].crDr // 方向
        var tmpBeginBalance = Number(arr[i].beginBalance) // 期初金额
        var tmpYearNetDr = Number(arr[i].yearNetDr) // 借方金额
        var tmpYearNetCr = Number(arr[i].yearNetCr) // 贷方金额
        if (tmpCrDr === 1) {
          obj.sumBalanceDr += tmpBeginBalance
        } else {
          obj.sumBalanceCr += tmpBeginBalance
        }
        obj.sumYearNetDr += tmpYearNetDr
        obj.sumYearNetCr += tmpYearNetCr
      }
      obj.diffBalance = obj.sumBalanceDr - obj.sumBalanceCr
      obj.diffYearNet = obj.sumYearNetDr - obj.sumYearNetCr
      this.balanceObj = obj
    },
    valChange(row) {
      this.$nextTick(() => {
        const crDr = row.crDr
        const balance = toNumStr(row.beginBalance)
        const balanceQty = toNumStr(row.beginBalanceQty)
        const yearNetDr = toNumStr(row.yearNetDr)
        const yearNetQtyDr = toNumStr(row.yearNetQtyDr)
        const yearNetCr = toNumStr(row.yearNetCr)
        const yearNetQtyCr = toNumStr(row.yearNetQtyCr)
        row.beginBalance = balance
        row.beginBalanceQty = balanceQty
        row.yearNetDr = yearNetDr
        row.yearNetQtyDr = yearNetQtyDr
        row.yearNetCr = yearNetCr
        row.yearNetQtyCr = yearNetQtyCr
        if (Number(crDr) === 1) {
          row.beginBalanceDr = balance
          row.beginBalanceQtyDr = balanceQty
          row.beginBalanceCr = 0
          row.beginBalanceQtyCr = 0
        } else {
          row.beginBalanceDr = 0
          row.beginBalanceQtyDr = 0
          row.beginBalanceCr = balance
          row.beginBalanceQtyCr = balanceQty
        }
        if (Number(row.type) === 0 && Number(row.leaf) === 1) {
          // 是辅助核算科目，或是末级科目
          this.calculate(row)
        } else {
          if (row.coaCode.length > 4) {
            this.calculateTop(row.coaCode)
          }
        }
      })
    },
    calculate(row) {
      var amount1 = 0
      var amount2 = 0
      var amount3 = 0
      var amount4 = 0
      var amount5 = 0
      var amount6 = 0
      var coaCode = row.coaCode
      const len = coaCode.length
      const parLen = this.getParentCodeLength(len)
      const parentCode = coaCode.substr(0, parLen)
      if (row.isAuxiliary === 1) {
        this.tableData.forEach((item) => {
          if (item.coaCode === coaCode && item.type === 0 && item.leaf === 1) {
            amount1 += Number(item.beginBalance)
            amount2 += Number(item.beginBalanceQty)
            amount3 += Number(item.yearNetDr)
            amount4 += Number(item.yearNetQtyDr)
            amount5 += Number(item.yearNetCr)
            amount6 += Number(item.yearNetQtyCr)
          }
        })
        this.tableData.forEach((item) => {
          if (item.coaCode === coaCode && item.type === 1 && item.leaf === 1) {
            this.$set(item, 'beginBalance', amount1)
            this.$set(item, 'beginBalanceQty', amount2)
            this.$set(item, 'yearNetDr', amount3)
            this.$set(item, 'yearNetQtyDr', amount4)
            this.$set(item, 'yearNetCr', amount5)
            this.$set(item, 'yearNetQtyCr', amount6)
            if (item.coaCode.length > 4) {
              this.calculateTop(row.coaCode)
            }
          }
        })
      } else {
        this.tableData.forEach((item) => {
          if (item.coaCode.substr(0, parLen) === parentCode && item.coaCode.length === len && item.type === 0 && item.leaf === 1) {
            amount1 += Number(item.beginBalance)
            amount2 += Number(item.beginBalanceQty)
            amount3 += Number(item.yearNetDr)
            amount4 += Number(item.yearNetQtyDr)
            amount5 += Number(item.yearNetCr)
            amount6 += Number(item.yearNetQtyCr)
          }
        })
        this.tableData.forEach((item) => {
          if (item.coaCode === parentCode && item.leaf === 0) {
            this.$set(item, 'beginBalance', amount1)
            this.$set(item, 'beginBalanceQty', amount2)
            this.$set(item, 'yearNetDr', amount3)
            this.$set(item, 'yearNetQtyDr', amount4)
            this.$set(item, 'yearNetCr', amount5)
            this.$set(item, 'yearNetQtyCr', amount6)
            if (item.coaCode.length > 4) {
              this.calculateTop(row.coaCode)
            }
          }
        })
      }
    },
    calculateTop(coaCode) {
      // 向上汇总
      const len = coaCode.length
      const parLen = this.getParentCodeLength(len)
      const parentCode = coaCode.substr(0, parLen)
      var amount1 = 0
      var amount2 = 0
      var amount3 = 0
      var amount4 = 0
      var amount5 = 0
      var amount6 = 0
      this.tableData.forEach((item) => {
        const flag = (item.type === 1 && item.isAuxiliary === 1) || (item.type === 0 && item.isAuxiliary === 0)
        if (item.coaCode.substr(0, parLen) === parentCode && item.coaCode.length === len && flag) {
          console.log(item.coaCode)
          amount1 += Number(item.beginBalance)
          amount2 += Number(item.beginBalanceQty)
          amount3 += Number(item.yearNetDr)
          amount4 += Number(item.yearNetQtyDr)
          amount5 += Number(item.yearNetCr)
          amount6 += Number(item.yearNetQtyCr)
        }
      })
      this.tableData.forEach((item) => {
        if (item.coaCode === parentCode) {
          console.log(amount1)
          this.$set(item, 'beginBalance', amount1)
          this.$set(item, 'beginBalanceQty', amount2)
          this.$set(item, 'yearNetDr', amount3)
          this.$set(item, 'yearNetQtyDr', amount4)
          this.$set(item, 'yearNetCr', amount5)
          this.$set(item, 'yearNetQtyCr', amount6)
          if (item.coaCode.length > 4) {
            console.log(item.coaCode)
            this.calculateTop(item.coaCode)
          }
        }
      })
    },
    getParentCodeLength(len) {
      const userInfo = JSON.parse(sessionStorage.userInfo)
      const rules = userInfo.book.codingRule.split('-')
      const newRules = rules.map((item, index) => {
        return this.getSum(rules, index)
      })
      const prevIndex = newRules.indexOf(len) - 1
      return newRules[prevIndex]
    },
    getSum(arr, index) {
      let sum = 0
      for (let i = 0; i <= index; i++) {
        sum += Number(arr[i])
      }
      return sum
    },
    showSuplyConfig(index) {
      this.curShowIndex = index
      this.form.auxiliary = this.tableData[index].auxiliary
      this.auxiliaryCoaArr = this.auxiliaryArr.filter(it => this.form.auxiliary.charAt(it.seq - 1) === '1')
      for (const key in this.resetform) {
        this.form[key] = this.resetform[key]
      }
      this.dialogVisible2 = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    saveAuxiliaryConfig() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          var editIndex = this.curShowIndex
          var curObj = deepClone(this.auxiliaryData[editIndex])
          var auxiliaryCode = ''
          var auxiliaryName = ''
          this.auxiliaryCoaArr.forEach(it => {
            const auxiliaryType = it.auxiliaryTypeCode
            const refName = auxiliaryType + 'Select'
            const modelCode = this.$refs[refName][0].getCode()
            const modelName = this.$refs[refName][0].getLabel()
            auxiliaryCode += '_' + it.serialNumber + modelCode
            auxiliaryName += '_' + modelName
            curObj[auxiliaryType] = this.$refs[refName][0].auxiliary
            curObj[auxiliaryType + 'Id'] = this.form[auxiliaryType + 'Id']
          })
          curObj.coaCobinationCode = auxiliaryCode.substring(1)
          curObj.coaCobinationName = auxiliaryName.substring(1)
          curObj.showCoaCode = curObj.coaCode + '_' + curObj.coaCobinationCode
          curObj.showCoaName = curObj.coaName + '_' + curObj.coaCobinationName
          curObj.leaf = 1
          curObj.type = 0
          curObj.isAuxiliary = 1
          curObj.beginBalance = this.form.beginBalance
          curObj.beginBalanceQty = this.form.beginBalanceQty
          curObj.yearNetDr = this.form.yearNetDr
          curObj.yearNetQtyDr = this.form.yearNetQtyDr
          curObj.yearNetCr = this.form.yearNetCr
          curObj.yearNetQtyCr = this.form.yearNetQtyCr
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.tableData.splice(this.curShowIndex + 1, 0, curObj)
              this.dialogVisible2 = false
              this.$nextTick(() => {
                this.calculate(curObj)
              })
            }
          })
        }
      })
    },
    saveData(type) {
      var info = ''
      this.getBalanceObj()
      if (this.balanceObj.diffBalance !== 0) {
        info = '借贷方期初余额不等,'
      }
      if (this.balanceObj.diffYearNet !== 0) {
        info = '借贷方发生额不等,'
      }
      this.saveloading = true
      this.auxiliaryData = deepClone(this.tableData)
      updateListForSetBegin(this.auxiliaryData).then((res) => {
        this.saveloading = false
        this.dialogVisible2 = false
        if (res.data.errorCode === '0') {
          if (type === 0) {
            this.$message.success(info + '期初余额保存完成!')
          }
          if (type === 1) {
            this.$message.success('辅助核算设置完成!')
          }
          if (type === 2) {
            this.$message.success('关闭辅助核算设置完成!')
          }
          this.getData()
        }
      }).catch(() => {
        this.saveloading = false
      })
    },
    getData() {
      this.listLoading = true
      var obj = {
        periodId: userInfo.book.enablePeriodId,
        periodCode: userInfo.book.enablePeriodCode,
        periodName: userInfo.book.enablePeriodName,
        periodYear: userInfo.book.enablePeriodYear,
        periodNum: userInfo.book.enablePeriodNum,
        coaHierarchyId: userInfo.book.coaHierarchyId
      }
      getBalance(obj).then((res) => {
        this.listLoading = false
        this.tableData = res.data.data || []
        this.auxiliaryData = this.tableData
      }).catch(() => {
        this.listLoading = false
      })
    }
  }
}
</script>
<style lang="scss" scoped>
input {
  display: block;
  width: 100%;
  height: 100%;
  font-size: 12px;
  padding-right: 8px;
  border: none;
}
input[disabled] {
  background: #f9f9f9;
}
>>> .tx-r input {
  text-align: right;
}
.label {
  font-size: 14px;
  margin-right: 5px;
  color: #333;
  margin-left: 10px;
}
>>> .el-table .cell {
  padding: 0;
  height: 32px;
}
>>> .el-table td {
  padding: 0px;
}

.label:first-child {
  margin-left: 0;
}
>>> .el-form-item {
  margin-bottom: 20px;
}

>>> .el-table.balance th {
  padding: 5px 0;
}
.tabletxt {
  display: inline-block;
  line-height: 32px;
}
>>> .el-button--mini.is-circle {
  padding: 4px;
}
>>> .auditform .el-form-item__label {
  line-height: 24px;
  padding-bottom: 0px;
}
.fileDiv {
  width: 400px;
  height: 60px;
  border: 1px #d0d0d0 solid;
  line-height: 60px;
  text-align: center;
  border-radius: 6px;
}
.filter-item {
  position: relative;
}
</style>
